/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.perutaxi.datos;

import com.perutaxi.entidades.eEmpresaTaxista;
import com.perutaxi.entidades.eTaxistaAuto;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author PeruTaxi
 */
public class dTaxistaAuto implements Serializable {

    public static eTaxistaAuto inicializar() {
        eTaxistaAuto taxAuto = new eTaxistaAuto();
        taxAuto.setEmpTaxista(new eEmpresaTaxista());
        taxAuto.setMarca("");
        taxAuto.setModelo("");
        taxAuto.setPlaca("");
        taxAuto.setColor("");
        taxAuto.setNroSoat("");
        taxAuto.setAnoFabricacion(2000L);
        taxAuto.setCapacidad(1L);
        taxAuto.setEstado(Boolean.TRUE);

        return taxAuto;
    }

    public static List<eTaxistaAuto> obtenerPorIdTaxista(Long id) throws Exception {
        List<eTaxistaAuto> lista = new ArrayList<eTaxistaAuto>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_AUTO WHERE TAXISTA = " + id;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eTaxistaAuto taxAuto = new eTaxistaAuto();
                taxAuto.setId(rs.getLong(1));
                taxAuto.setMarca(rs.getString(3));
                taxAuto.setModelo(rs.getString(4));
                taxAuto.setPlaca(rs.getString(5));
                taxAuto.setAnoFabricacion(rs.getLong(6));
                taxAuto.setColor(rs.getString(7));
                taxAuto.setNroSoat(rs.getString(8));
                taxAuto.setCapacidad(rs.getLong(9));
                taxAuto.setEstado(rs.getBoolean(10));
                lista.add(taxAuto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static eTaxistaAuto obtenerPorId(Long id) throws Exception {
        eTaxistaAuto taxAuto = new eTaxistaAuto();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_AUTO WHERE ID = " + id;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                taxAuto.setId(rs.getLong(1));
                taxAuto.setMarca(rs.getString(3));
                taxAuto.setModelo(rs.getString(4));
                taxAuto.setPlaca(rs.getString(5));
                taxAuto.setAnoFabricacion(rs.getLong(6));
                taxAuto.setColor(rs.getString(7));
                taxAuto.setNroSoat(rs.getString(8));
                taxAuto.setCapacidad(rs.getLong(9));
                taxAuto.setEstado(rs.getBoolean(10));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return taxAuto;
    }

    public static List<eTaxistaAuto> obtenerTodos() throws Exception {
        List<eTaxistaAuto> lista = new ArrayList<eTaxistaAuto>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_AUTO";
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eTaxistaAuto ent = new eTaxistaAuto();
                ent.setId(rs.getLong(1));
                ent.setMarca(rs.getString(3));
                ent.setModelo(rs.getString(4));
                ent.setPlaca(rs.getString(5));
                ent.setAnoFabricacion(rs.getLong(6));
                ent.setColor(rs.getString(7));
                ent.setNroSoat(rs.getString(8));
                ent.setCapacidad(rs.getLong(9));
                ent.setEstado(rs.getBoolean(10));
                lista.add(ent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static boolean guardar(eTaxistaAuto taxAuto) throws Exception {
        boolean retorno = true;
        Connection cnx = Conexion.getConexion();
        String query = "INSERT INTO TAXISTA_AUTO VALUES (?,?,?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, taxAuto.getId());
            ps.setLong(2, taxAuto.getEmpTaxista().getId());
            ps.setString(3, taxAuto.getMarca());
            ps.setString(4, taxAuto.getModelo());
            ps.setString(5, taxAuto.getPlaca());
            ps.setLong(6, taxAuto.getAnoFabricacion());
            ps.setString(7, taxAuto.getColor());
            ps.setString(8, taxAuto.getNroSoat());
            ps.setLong(9, taxAuto.getCapacidad());
            ps.setBoolean(10, taxAuto.getEstado());
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            retorno = false;
        } finally {
            cnx.close();
        }

        return retorno;
    }

    public static boolean actualizar(eTaxistaAuto taxAuto) throws Exception {
        boolean retorno = true;
        Connection cnx = Conexion.getConexion();
        String query = "UPDATE TAXISTA_AUTO SET TAXISTA = ?, MARCA = ?, MODELO = ?, PLACA = ?, ANO_FABRICACION = ?, COLOR = ?, NRO_SOAT = ?, ";
        query += "CAPACIDAD = ?, ESTADO = ? WHERE ID = ?";
        try {
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, taxAuto.getEmpTaxista().getId());
            ps.setString(2, taxAuto.getMarca());
            ps.setString(3, taxAuto.getModelo());
            ps.setString(4, taxAuto.getPlaca());
            ps.setLong(5, taxAuto.getAnoFabricacion());
            ps.setString(6, taxAuto.getColor());
            ps.setString(7, taxAuto.getNroSoat());
            ps.setLong(8, taxAuto.getCapacidad());
            ps.setBoolean(9, taxAuto.getEstado());
            ps.setLong(10, taxAuto.getId());
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            retorno = false;
        } finally {
            cnx.close();
        }

        return retorno;
    }
}
